home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / winsr173.zip / F16.C < prev    next >
Text File  |  1990-10-04  |  3KB  |  89 lines

  1. /**************************************
  2. **
  3. ** F16.C : Code to read 16-bit fractal data sets.  Uses
  4. ** strictly Targa16 type 10 files (run-length encoded 16-bit RGB).
  5. */
  6.  
  7. /* Lee Daniel Crocker       CompuServe: 73407,2030   <== Preferred
  8. ** 1380 Jewett Ave.          BIX: lcrocker
  9. ** Pittsburg, CA  94565        Usenet: ...!ames!pacbell!sactoh0!siva!lee
  10. **
  11. ** This code is hereby placed in the public domain.  You are free to
  12. ** use, modify, usurp, laugh at, destroy, or otherwise abuse it in any
  13. ** way you see fit.
  14. **
  15. ** "If you steal from one author it's plagiarism; if you steal from
  16. ** many it's research."  --Wilson Mizner
  17. */
  18.  
  19. /* 16 bit .tga files were generated for continuous potential "potfile"s
  20.    from version 9.? thru version 14.  Replaced by double row gif type
  21.    file (.pot) in version 15.  Delete this code after a few more revs.
  22.    The part which wrote 16 bit .tga files has already been deleted.
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "port.h"
  29. #include "targa_lc.h"
  30.  
  31. extern char rlebuf[258];    /* RLE-state variables */
  32. static int state, count, bufp;
  33.  
  34. /**************************************
  35. **
  36. ** Open previously saved Targa16 type 10 file filling in hs, vs, and
  37. ** csize with values in the header.  If *csize is not zero, the block
  38. ** pointed to by cp is filled with the comment block.  The caller
  39. ** _must_ allocate 256 bytes for this purpose before calling.
  40. */
  41.  
  42. FILE *t16_open(char *fname, int *hs, int *vs, int *csize, U8 *cp)
  43. {
  44.     char filename[64];
  45.     U8 header[HEADERSIZE];
  46.     FILE *fp;
  47.  
  48.     strcpy(filename, fname);
  49.     if (strchr(filename, '.') == NULL) strcat(filename, ".TGA");
  50.     if ((fp = fopen(filename, READMODE)) == NULL) return NULL;
  51.  
  52.     fread(header, HEADERSIZE, 1, fp);
  53.     if ((header[O_FILETYPE] != T_RLERGB) || (header[O_ESIZE] != 16)) {
  54.     fclose(fp);
  55.     return NULL;
  56.     }
  57.     GET16(header[O_HSIZE], *hs);
  58.     GET16(header[O_VSIZE], *vs);
  59.     if (*csize = header[O_COMMENTLEN]) fread(cp, *csize, 1, fp);
  60.  
  61.     state = count = bufp = 0;
  62.     return fp;
  63. }
  64.  
  65. int t16_getline(FILE *fp, int hs, U16 *data)
  66. {
  67.     int i;
  68.  
  69.     for (i=0; i<hs; ++i) {
  70.     if (state == 0) {
  71.         bufp = 0;
  72.         if ((count = getc(fp)) > 127) {
  73.         state = 1;
  74.         count -= 127;
  75.         fread(rlebuf, 2, 1, fp);
  76.         } else {
  77.         state = 2;
  78.         ++count;
  79.         fread(rlebuf, 2, count, fp);
  80.         }
  81.     }
  82.     GET16(rlebuf[bufp], data[i]);
  83.     if (--count == 0) state = 0;
  84.     if (state == 2) bufp += 2;
  85.     }
  86.     return 0;
  87. }
  88.  
  89.